#include <string>
#include "Curses.h"
#include "Item.h"
#include "InventoryWindow.h"

const int itemsPerPage = 20;

InventoryWindow::InventoryWindow(Inventory* inventory)
 : Window(30, 22, 1, 1, true), m_inventory(inventory)
{
}

void InventoryWindow::show() {
	Curses curses = Curses::instance();
	
	curses.pushWindow(this);
	
	int page = 1;
	int key = 0;
	while (key != '\n') {
		// TODO: Stack together "same" items.
		
		for (int i = 0; i < itemsPerPage; i++) {
			if (i + (page - 1)*itemsPerPage < m_inventory->size()) {
				std::string str = std::string(" - ") + m_inventory->item(i + (page - 1)*itemsPerPage)->name();
				
				char symbol = m_inventory->itemSymbol(m_inventory->item(i + (page - 1)*itemsPerPage));
				
				curses.putchar(this, 1, 1 + i, symbol);
				curses.print(this, 2, 1 + i, str);
			}
		}
		
		curses.print(this, 1, height() - 1, "Space - Next page");
		
		curses.refresh();
		key = curses.getchar();
		
		if (key == ' ') {
			if (m_inventory->size() <= page * itemsPerPage) {
				// Exit inventory if there's not enough items.
				break;
			} else {
				page++;
				clear();
			}
		}
		
	}
	
	curses.popWindow();
	
	curses.clearScreen();
	curses.refresh();
}